<!-- 查询区 -->
<div id="searchContainer"></div>
<div class="row1" style="margin-top:10px;">
    <div class="data_container">
        <!-- 按钮区 -->
        <div id="buttonsDiv">
            <input type="button" class="bh-btn bh-btn-primary" btn-action="edit" value="新增" />
        </div>
        <!-- 数据列表区 -->
        <div id="contentDiv" class="content_div"></div>
    </div>
</div>
<script>
$(function() {
    $("#buttonsDiv").posfixed({ distance: 55, pos: "top", type: "while", hide: false });

    // 加载查询区
    var searchBar = new SearchBar({
        containerId: "searchContainer",
        queryList:[
            { title: "是否启用", key:"enabled", data:{ key:"enabled", datas:[{ code:"1", name:"是" },{ code:"0", name:"否" }]} }
        ],
        queryFunc: function() { load(); }
    });
    searchBar.render();

    // 加载表格
    var workGrid = new WorkGrid({
        url: '/upgradeProject/query',
        searchBar: searchBar,
        columns: [
            { display: "代码", property: "code", sort: "code", width: "12%" }, 
            { display: "项目名称", property: "name", sort: "name", width: "20%" }, 
            { display: "升级包名", property: "package_name", sort: "package_name", width: "18%" }, 
            { display: "文件夹根目录", property: "package_base_dir", sort: "package_base_dir", width: "12%" }, 
            { display: "备注", property: "remark", sort: "remark", width: "10%" }, 
            { display: "是否启用", sort: "enabled", width: "8%", view: function(col) {
                if (col.enabled == 1) return "<div class='tag accepted'>启用</div>";
                else return "<div class='tag rejected'>禁用</div>";
            }}, 
            { display: "操作", view: function(col) {
                var view = '<a href="javascript:void(0);" data-action="edit" data-val="' + col.id + '">修改</a> ';
                view += '<a href="javascript:void(0);" data-action="remove" data-val="' + col.id + '">删除</a> ';
                view += '<a>绑定学校</a> ';
                return view;
            }}
        ],
        loadAfterListener: function() {
            $('#contentDiv').find('a[data-action="edit"]').click(function(e) {
                var val = $(e.currentTarget).attr('data-val');
                edit(val);
            });
            $('#contentDiv').find('a[data-action="remove"]').click(function(e) {
                var val = $(e.currentTarget).attr('data-val');
                $.confirm({
                    title: 'Confirm!',
                    content: '确认删除选择的项目吗？',
                    confirm: function() {
                        $.ajax({
                            url: '/upgradeProject/remove', dataType: "json", cache: false, type: "post", data: 'id=' + val,
                            success: function(data) {
                                if (data.success) {
                                    $.toast({ text: '删除成功！', showHideTransition: 'slide', icon: 'success', position: 'top-center' });
                                } else {
                                    $.toast({ text: '删除失败！', showHideTransition: 'slide', icon: 'error', position: 'top-center' });
                                }
                                load();
                            }
                        });
                    }
                });
            });
        }
    });
    workGrid.render();

    // 绑定按钮区事件
    $('[btn-action="edit"]').click(function() {
        edit();
    });

    // 查询
    function load(params) {
        workGrid.load(1, params);
    }

    function edit(id) {
        $.confirm({
            content: 'url:/upgradeProject/edit?id=' + (id || '') + '&_=' + new Date().getTime(),
            title: id ? '修改升级项目' : '新增升级项目',
            columnClass: 'col-md-8 col-md-offset-2',
            confirm: function() {
                if (!$("#myEditForm").valid())
                    return false;

                $.ajax({
                    url: '/upgradeProject/save', dataType: "json", cache: false, type: "post",
                    data: $('#myEditForm').serialize(),
                    success: function(data) {
                        if (data.success) {
                            $.toast({ text: '保存成功！', showHideTransition: 'slide', icon: 'success', position: 'top-center' });
                        } else {
                            $.toast({ text: '保存失败！', showHideTransition: 'slide', icon: 'error', position: 'top-center' });
                        }
                        load();
                    }
                });
            }
        });
    }
});
</script>
